package com.ibm.tivoli.swat.tsam_ec2.ec2;

import java.util.ArrayList;

import com.amazonaws.ec2.DescribeAvailabilityZonesResponseType;
import com.amazonaws.ec2.DescribeImagesResponseInfoType;
import com.amazonaws.ec2.DescribeImagesResponseItemType;
import com.amazonaws.ec2.DescribeImagesResponseType;
import com.amazonaws.ec2.DescribeInstancesResponseType;
import com.amazonaws.ec2.DescribeKeyPairsResponseType;
import com.amazonaws.ec2.DescribeSecurityGroupsResponseType;
import com.amazonaws.ec2.GroupSetType;
import com.amazonaws.ec2.InstanceMonitoringStateType;
import com.amazonaws.ec2.InstanceStateChangeSetType;
import com.amazonaws.ec2.InstanceStateType;
import com.amazonaws.ec2.PlacementResponseType;
import com.amazonaws.ec2.RebootInstancesResponseType;
import com.amazonaws.ec2.ReservationInfoType;
import com.amazonaws.ec2.ReservationSetType;
import com.amazonaws.ec2.RunInstancesResponseType;
import com.amazonaws.ec2.RunningInstancesItemType;
import com.amazonaws.ec2.RunningInstancesSetType;
import com.amazonaws.ec2.SecurityGroupSetType;
import com.amazonaws.ec2.TerminateInstancesResponseType;
import com.ibm.tivoli.swat.tsam_ec2.tsam.TSAMWebServiceClient;
import com.ibm.tivoli.swat.tsam_ec2.tsam.TSAMWebServiceClientException;
import com.ibm.tivoli.swat.tsam_ec2.tsam.model.TSAMImage;
import com.ibm.tivoli.swat.tsam_ec2.tsam.model.TSAMInstance;

public class TSAMEC2Impl implements TSAMEC2 {

	private TSAMWebServiceClient client;

	private static TSAMEC2Impl singleton = new TSAMEC2Impl();

	private TSAMEC2Impl() {
		client = TSAMWebServiceClient.getInstance();
	}

	public static TSAMEC2Impl getInstance() {
		return singleton;
	}

	private String getRequestId() {
		return "TSAMRequestId";
	}

	@Override
	public DescribeImagesResponseType describeImages()
			throws TSAMWebServiceClientException {
		ArrayList<TSAMImage> list = client.describeImages();
		DescribeImagesResponseType type = new DescribeImagesResponseType();
		type.setRequestId(getRequestId());
		DescribeImagesResponseInfoType infoType = new DescribeImagesResponseInfoType();
		type.setImagesSet(infoType);
		for (TSAMImage image : list) {
			DescribeImagesResponseItemType itemType = new DescribeImagesResponseItemType();
			infoType.getItem().add(itemType);
			itemType.setName(image.getName());
			itemType.setImageLocation(image.getName());
			itemType.setImageId(image.getImageId());
			itemType.setImageState(image.getImageState());
			itemType.setImageOwnerId(image.getImageOwnerId());
			itemType.setPlatform(image.getPoolIdentifier());
		}
		return type;
	}

	public DescribeSecurityGroupsResponseType describeSecurityGroups() {
		DescribeSecurityGroupsResponseType type = new DescribeSecurityGroupsResponseType();
		type.setRequestId(getRequestId());

		SecurityGroupSetType info = new SecurityGroupSetType();
		type.setSecurityGroupInfo(info);

		return type;
	}

	public DescribeInstancesResponseType describeInstances()
			throws TSAMWebServiceClientException {
		ArrayList<TSAMInstance> list = client.describeInstances();
		DescribeInstancesResponseType type = new DescribeInstancesResponseType();
		type.setRequestId(getRequestId());

		ReservationSetType reservation = new ReservationSetType();
		type.setReservationSet(reservation);

		for (TSAMInstance instance : list) {
			ReservationInfoType info = new ReservationInfoType();
			info.setReservationId(instance.getReservationId());
			info.setOwnerId(instance.getOwnerId());

			GroupSetType groupSet = new GroupSetType();
			info.setGroupSet(groupSet);

			RunningInstancesSetType instancesSet = new RunningInstancesSetType();
			info.setInstancesSet(instancesSet);

			RunningInstancesItemType runningInstance = new RunningInstancesItemType();
			instancesSet.getItem().add(runningInstance);

			runningInstance.setImageId(instance.getImageId());
			runningInstance.setInstanceId(instance.getInstanceId());
			runningInstance.setPrivateDnsName(instance.getPrivateDnsName());
			runningInstance.setInstanceType(instance.getInstanceType());
			runningInstance.setLaunchTime(instance.getLaunchTime());
			runningInstance.setGroupSet(groupSet);

			InstanceStateType state = new InstanceStateType();
			state.setName(instance.getStatus());
			runningInstance.setInstanceState(state);

			InstanceMonitoringStateType monitoring = new InstanceMonitoringStateType();
			monitoring.setState(instance.getMonitoringState());
			runningInstance.setMonitoring(monitoring);

			PlacementResponseType placement = new PlacementResponseType();
			placement.setAvailabilityZone(instance.getAvailabilityZone());
			runningInstance.setPlacement(placement);

			reservation.getItem().add(info);
		}

		return type;
	}

	@Override
	public RebootInstancesResponseType rebootInstances(String instanceId)
			throws TSAMWebServiceClientException {
		client.rebootInstance(instanceId);

		RebootInstancesResponseType result = new RebootInstancesResponseType();
		result.setReturn(true);
		return result;
	}
	
	@Override
	public DescribeKeyPairsResponseType describeKeyPairs() {
		DescribeKeyPairsResponseType result = new DescribeKeyPairsResponseType ();
		return result;
	}
	
	@Override
	public DescribeAvailabilityZonesResponseType describeAvailabilityZones() {
		return new DescribeAvailabilityZonesResponseType ();
	}
	
	@Override
	public RunInstancesResponseType runInstances (String imageId) throws TSAMWebServiceClientException {
		client.runInstance(imageId);
		
		RunInstancesResponseType result = new RunInstancesResponseType ();
		RunningInstancesSetType set = new RunningInstancesSetType ();
		result.setInstancesSet(set);
		
		return result;
	}

	@Override
	public TerminateInstancesResponseType terminateInstances(String instanceId)
			throws TSAMWebServiceClientException {
		client.terminateInstance(instanceId);

		TerminateInstancesResponseType result = new TerminateInstancesResponseType();
		InstanceStateChangeSetType set = new InstanceStateChangeSetType ();
		result.setInstancesSet(set);
		return result;
	}

}
